package com.plian.system.config;

import com.alibaba.ttl.threadpool.TtlExecutors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @description:
 * @author: gangan
 * @create: 2021-01-07 15:29
 */
@Configuration
@EnableAsync
public class ExecutorConfiguration implements AsyncConfigurer {

    @Bean("ttlExecutor")
    public Executor importExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadFactory(r -> executor.createThread(Objects.requireNonNull(TtlRunnable.get(r))));
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(200);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("ttlExecutor-");
        executor.setAllowCoreThreadTimeOut(Boolean.TRUE);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return TtlExecutors.getTtlExecutor(executor);
    }

    @Override
    public Executor getAsyncExecutor() {
        return importExecutor();
    }
}
